package com.techsen.tsweb.sys.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.sql.Pager;
import com.techsen.tsweb.sys.domain.UserRole;
import com.techsen.tsweb.sys.service.UserRoleService;

@Controller
@RequestMapping("/sys/userrole")
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;
    
    /**
     * 进入用户角色管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/userrole";
    }
    
    /**
     * 分页查询用户角色
     */
    @RequestMapping("/pager")
    public @ResponseBody Pager<UserRole> pager(@RequestBody Pager<UserRole> pager) {
        return this.userRoleService.findByPager(pager);
    }
    
    /**
     * 进入添加用户角色界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "sys/userrole-add";
    }
    
    /**
     * 添加用户角色
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(UserRole userrole) {
        this.userRoleService.add(userrole);
        return new JsonResult(userrole);
    }
    
    /**
     * 删除用户角色
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        this.userRoleService.delete(id);
        return new JsonResult(id);
    }
    
    /**
     * 进入修改用户角色界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String userroleId, Model model) {
        UserRole userrole = this.userRoleService.get(userroleId);
        model.addAttribute("userrole", userrole);
        return "sys/userrole-update";
    }
    
    /**
     * 修改用户角色
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult update(UserRole userrole) {
        this.userRoleService.update(userrole);
        return new JsonResult(userrole);
    }
    
}
